package cn.csxw.openchina.ui.adapter;

import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.constant.Constant;
import cn.csxw.openchina.entity.bean.LikeListBean;
import cn.csxw.openchina.entity.bean.TweetBean;
import cn.csxw.openchina.ui.activity.MoveCommentActivity;
import cn.csxw.openchina.utils.Global;

/**
 * 动弹的Adapter
 */

public class MoveAdapter extends BaseRecAdapter<TweetBean> {

    @Override
    protected View getItemView(ViewGroup parent) {
        View itemView = Global.inflate(R.layout.view_move_item, parent);
        return itemView;
    }

    @Override
    protected BaseViewHolder getViewHolder(View itemView) {
        return new MoveHolder(itemView);
    }

    @Override
    protected void onItemClick(View v, int position) {
        TweetBean tweetBean = mData.get(position);
        Intent intent = new Intent();
        intent.putExtra(Constant.TWEETBEAN_INFO,tweetBean);
        intent.putExtra(Constant.COMMENT_POSITION,position);
        intent.setClass(Global.mContext, MoveCommentActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Global.mContext.startActivity(intent);
    }

    //点击的Adapter，解决复用问题
    private  List<String> checkPositionList = new ArrayList<>();

    public class MoveHolder extends BaseViewHolder {

        @BindView(R.id.iv_move_item_photo)
        ImageView mMoveItemPhoto;
        @BindView(R.id.tv_move_item_nikeName)
        TextView mMoveItemNikeName;
        @BindView(R.id.tv_move_item_des)
        TextView mMoveItemDes;
        @BindView(R.id.tv_move_item_time)
        TextView mMoveItemTime;
        @BindView(R.id.tv_move_item_count)
        TextView mMoveItemCount;
        @BindView(R.id.iv_move_item_unlike)
        ImageView mMoveItemUnlike;
        @BindView(R.id.ll_move_item_right)
        LinearLayout mMoveItemRight;
        @BindView(R.id.tv_move_item_like)
        TextView mMoveItemLike;
        TweetBean tweetBean;

        public MoveHolder(View itemView) {
            super(itemView);
            //将每个条目的位置保存
            mMoveItemUnlike.setTag(String.valueOf(getLayoutPosition()));
        }

        @Override
        public void setData(TweetBean tweetBean, int Position) {
            this.tweetBean = tweetBean;
            String body = tweetBean.body;
            Spanned bodySpanned = Html.fromHtml(body);
            //用户图片
            Glide.with(Global.mContext).load(tweetBean.portrait).into(mMoveItemPhoto);

            mMoveItemDes.setText(bodySpanned);
            mMoveItemNikeName.setText(tweetBean.author);
            mMoveItemTime.setText(tweetBean.pubDate);
            mMoveItemCount.setText(String.valueOf(tweetBean.commentCount));
            List<LikeListBean> likeList = tweetBean.likeList;
            if (likeList != null && likeList.size() > 0) {
                StringBuffer sb = new StringBuffer();
                mMoveItemLike.setVisibility(View.VISIBLE);
                for (int i = 0; i < likeList.size(); i++) {
                    if (likeList.size() > 1) {
                        if(i!= likeList.size()-1){
                            sb.append(tweetBean.likeList.get(i).name);
                            sb.append("、");
                        }else{
                            sb.append(tweetBean.likeList.get(i).name);
                        }
                 /*       if (likeList.size() == i + 1) {
                            sb.append(tweetBean.likeList.get(i).name);
                        }*/

                    }
                    else{
                        sb.append(tweetBean.likeList.get(i).name);
                    }

                    /*else {

                }*/
                }
                String name = sb.toString();
                name = getHasColorText(name) + String.valueOf("觉得很赞");
                Spanned nameSpanned = Html.fromHtml(name);
                mMoveItemLike.setText(nameSpanned);
            } else {
                mMoveItemLike.setVisibility(View.GONE);
            }


            //设置点赞状态是否存在
            //已经被选中，去判断集合里时候有Tag
            if (checkPositionList.contains(String.valueOf(getLayoutPosition()))){
                mMoveItemUnlike.setSelected(true);
            }else {
                mMoveItemUnlike.setSelected(false);
            }
        }

        @OnClick(R.id.iv_move_item_unlike)
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_move_item_unlike:
                    like(view);
                    break;
            }
        }

        private void like(View view) {
            //赞按钮的点击事件
            if (view.isSelected()) {
                //已经被选中了
                view.setSelected(false);
                checkPositionList.remove(String.valueOf(getLayoutPosition()));
                removeLike();
            } else {
                //没有被选中
                addLike(view);
                view.setSelected(true);
                checkPositionList.add(String.valueOf(getLayoutPosition()));
            }
            //更新条目的状态
            // notifyItemChanged(getLayoutPosition());
            notifyDataSetChanged();
        }


        //移除赞的方法
        private void removeLike() {
            for (int i = 0; i < tweetBean.likeList.size(); i++) {
                if ("sky".equals(tweetBean.likeList.get(i).name)) {
                    tweetBean.likeList.remove(i);
                }
            }
        }

        //点赞的方法
        private void addLike(View view) {
            setAnimal(view);
            LikeListBean listBean = new LikeListBean();
            listBean.name = "sky";
            tweetBean.likeList.add(listBean);
        }
        /**
         * 设置点赞的点击事件
         */
        private void setAnimal(View view) {
            ScaleAnimation animation =new ScaleAnimation(1f, 2f, 1f, 2f,
                    ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(500);
            animation.setFillAfter(false);
            animation.setRepeatCount(1);
            view.startAnimation(animation);
        }
    }

    /**
     * 设置富文本的字体颜色
     *
     * @param string
     * @return
     */
    private String getHasColorText(String string) {
        return String.format("<font color=\"#45BCE7\">%s</font>", string); // string 会替换 %s
    }
}
